﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.Domain;
using Sausage.Web.Models;
using Sausage.Web.Code;
using Sausage.Web.DataModel;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 工程计划控制器
    /// </summary>
    public class ProjectPlanController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Submit()
        {
            return View();
        }

        /// <summary>
        /// 修改工程计划。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Modify(Guid projectID)
        {
            //查询项目计划
            ProjectPlan model = ProjectPlanHelper.GetRawProjectPlanByProjectID(projectID);

            //判断模型是否存在
            if (null != model)
                return View("Submit", model); //导航到视图
            else
                //导航到视图
                return View("PlanNotFound", ProjectHelper.GetProjectByID(projectID));
        }

        /// <summary>
        /// 审核。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Audit()
        {
            return View();
        }

        /// <summary>
        /// 确认。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Confirm()
        {
            return View();
        }


        /// <summary>
        /// 签审。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Sign()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = ProjectPlanHelper.Select(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "工程计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SubmitSelect(JosnPagingQueryModel query)
        {
            var result = ProjectPlanHelper.SubmitSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "工程计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult AuditSelect(JosnPagingQueryModel query)
        {
            var result = ProjectPlanHelper.AuditSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "工程计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ConfirmSelect(JosnPagingQueryModel query)
        {
            var result = ProjectPlanHelper.ConfirmSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "工程计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询工程量数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程量信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SignSelect(JosnPagingQueryModel query)
        {
            var result = ProjectPlanHelper.SignSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "工程计划加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查看工程计划。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewPlan(Guid planID)
        {
            //返回视图
            return View(ProjectPlanHelper.GetProjectPlanByID(planID));
        }

        /// <summary>
        /// 查看工程计划。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewProjectPlan(Guid projectID)
        {
            //查询项目计划
            ProjectPlanListRow model = ProjectPlanHelper.GetProjectPlanByProjectID(projectID);

            //判断模型是否存在
            if (null != model)
                return View("ViewPlan", model); //导航到视图
            else //导航到视图
                return View("PlanNotFound", ProjectHelper.GetProjectByID(projectID));
        }

        /// <summary>
        /// 查看工程计划履历。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewProjectPlanHistory(Guid projectID)
        {
            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //查询工程信息
                Project model = context.Project.Include("Organization").Include("ProjectPlan").Where(item => item.ProjectID == projectID).FirstOrDefault();

                //导航到视图
                return View(model);
            }
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid id)
        {
            var item = ProjectPlanHelper.GetProjectPlanByID(id);
            if (item == null)
                return Json(new JsonResultViewModel { success = true, message = "找不到此工程计划！" },
                            JsonRequestBehavior.AllowGet);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    total = 1,
                    data = item,
                    message = "加载工程计划成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(ProjectPlanListRow data)
        {
            ProjectPlanHelper.DeleteProjectPlan(data.PlanID);
            return Json(new JsonResultViewModel { success = true, message = "工程计划删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(ProjectPlanListRow data)
        {
            ProjectPlanHelper.UpdateProjectPlan(data);
            return Json(new JsonResultViewModel { success = true, message = "工程计划更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 修订计划
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Revision(ProjectPlanListRow data)
        {
            if (ProjectPlanHelper.ProjectHasConfirmedPlan(data.ProjectID))
                return Json(new JsonResultViewModel { success = false, message = "此项目计划已经填写,还未审批,请修改未审批的项目计划!" }, JsonRequestBehavior.AllowGet);

            ProjectPlanHelper.UpdateToNewProjectPlan(data);
            return Json(new JsonResultViewModel { success = true, message = "工程计划更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult UpdateState(Guid id, int state)
        {
            ProjectPlanHelper.UpdateProjectPlanState(id, state);
            return Json(new JsonResultViewModel { success = true, message = "工程计划状态更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns>
        /// 创建结果。
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(ProjectPlanListRow data)
        {
            if (ProjectPlanHelper.ProjectHasPlan(data.ProjectID))
                return Json(new JsonResultViewModel { success = false, message = "此项目计划已经填写,不能再次填写,请修改已填写的项目计划!" }, JsonRequestBehavior.AllowGet);

            ProjectPlanHelper.CreateProjectPlan(data);
            return Json(new JsonResultViewModel { success = true, message = "工程计划创建成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="quantityID">The quantity ID.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSubmit(Guid[] plans)
        {
            ProjectPlanHelper.ExecuteSubmit(plans);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "工程计划提交成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="plans">The plans.</param>
        /// <param name="name">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteAudit(Guid[] plans, bool passed, string comment)
        {
            ProjectPlanHelper.ExecuteAudit(plans, passed, comment);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "工程计划审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 签署
        /// </summary>
        /// <param name="plans">The plans.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSign(Guid[] plans, bool passed)
        {
            ProjectPlanHelper.ExecuteSign(plans, passed);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "工程计划审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 批准
        /// </summary>
        /// <param name="plans">The plans.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteConfirm(Guid[] plans, bool passed, string comment)
        {
            ProjectPlanHelper.ExecuteConfirm(plans, passed, comment);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "工程计划审核成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
